import Vue from 'vue'
import Vuex from 'vuex'
import router from '../router'
import utils from '../utils/utils'
import axios from 'axios';
import { Message } from "element-ui";


Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    user: utils.getStorage('user') || {}
  },
  mutations: {
    setUser(state, user) {
      state.user = user
    }
  },
  actions: {
    loginUser(obj, user) {
      // 在这里发起ajax请求 
      axios({
        method: 'post',
        url: '/index/login',
        //post用data get用params
        data: user
      })
        .then((res) => {
          if (res.data.code == 200) {
            if (res.data.data[0].mylock == '1') {
              let userdata=res.data.data[0]
              Message({
                message: '登录成功',
                type: 'success'
              });
              // obj.commit('mutation里的函数名')
              obj.commit('setUser', userdata)
              // sessionStorage 浏览器的本地储存
              utils.setStorage('user', userdata)
              if (userdata.operation_Level=='99') {
                router.push('/home1/yuangongguanli')
              } else {
                router.push('/home2/shanghuo')
              }
            } else {
              Message({
                message: '该账号已锁定，请联系管理员解锁！',
                type: 'error'
              });
            }
            
          } else {
            Message({
              message: '用户名或密码错误！',
              type: 'error'
            });
          }
        })
        .catch((err) => {
          console.log(err);
        })

    }
  },
  modules: {
  }
})
